/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.context.FacesContext;
import model.*;

/**
 *
 * @author HELLO
 */
@Named(value = "u_memberDetailManagedBean")
@SessionScoped
public class MemberDetailManagedBean implements Serializable {
    
    @EJB
    private FriendFacade friendFacade;
    @EJB
    private Member1Facade member1Facade;
   
    private Member1 member = new Member1();

    public Member1 getMember() {
        return member;
    }

    public void setMember(Member1 member) {
        this.member = member;
    }
    
    public MemberDetailManagedBean() {
    }
    
    public void ViewDetail(Member1 m){
        this.member = m;
        this.goRedirect("memberdetail.xhtml");
    }
    
    public void ViewDetal(){
        this.goRedirect("memberdetail.xhtml");
    }
    
    public String Get_Avatar(Member1 m){
        if(m.getAvatar() == null || m.getAvatar()==""){
            return "default_avatar.jpg";
        }
        else return m.getAvatar();
    }
    
    public String Get_Avatar(){
        if(member.getAvatar() == null || member.getAvatar()==""){
            return "default_avatar.jpg";
        }
        else return member.getAvatar();
    }
    
    public int Get_FriendStatus(){
        MemberManagedBean mmb = get_MemberManagedBean();
        if(member1Facade.isFriend(mmb.getMember(), this.member)){
            return 1;
        }
        else if(member1Facade.isRequestingFriend(mmb.getMember(), this.member)){
            return 2;
        }
        else if(member1Facade.isRequestedFriend(mmb.getMember(), this.member)){
            return 3;
        }
        else{
            return 4;
        }
    }
    
    public void Friend_Action(int code){
        MemberManagedBean mmb = get_MemberManagedBean();
        if(code == 1){
            Friend f = friendFacade.GetFriendByMemberID(mmb.getMember().getId(), member.getId());
            this.friendFacade.remove(f);
        }
        else if(code == 2){
            for(Friend f : mmb.getMember().getFriendList1()){
                if(f.getIDMember().getId() == this.member.getId()){
                    this.friendFacade.remove(f);
                    break;
                }
            }
        }
        else if(code == 3){
            for(Friend f : mmb.getMember().getFriendList()){
                if(f.getIDFriend().getId() == this.member.getId()){
                    f.setRequest(true);
                    this.friendFacade.edit(f);
                    break;
                }
            }
        }
        else if(code == 4){
            for(Friend f : mmb.getMember().getFriendList()){
                if(f.getIDFriend().getId() == this.member.getId()){
                    this.friendFacade.remove(f);
                    break;
                }
            }
        }
        else if(code == 5){
            Friend f = new Friend();
            f.setIDFriend(mmb.getMember());
            f.setIDMember(member);
            f.setRequest(false);
            f.setRequestContext("Hello World!");
            this.friendFacade.create(f);
        }
        this.member1Facade.flush();
        mmb.RefreshMember();
        mmb.refreshFriendListCombined();
    }
    
    public void goRedirect(String page) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(page);
        } catch (Exception ex) {
        }
    }
    
    private MemberManagedBean get_MemberManagedBean() {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        MemberManagedBean mmb = (MemberManagedBean) FacesContext.getCurrentInstance().getApplication()
                .getELResolver().getValue(elContext, null, "u_memberManagedBean");
        return mmb;
    }
}
